﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ChangeColor.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Modifier
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT.ParticleSystem.Common;

    #endregion

    /// <summary>
    /// The ChangeColor Modifier alters the color of the particle as it ages.
    /// It uses the particle's energy level to decide what color to display.<br/>
    /// <br/>
    /// Usually a particle's energy changes from 1.0f to 0.0f over its lifetime, but
    /// this can be altered via the easing function set within the age modifier.<br/>
    /// <br/>
    /// This modifier should be used in conjunction with the Age action.
    /// </summary>
    public class ChangeColor : ModifierBase
    {
        #region Member Variables
        private Color _startColor;
        private Color _endColor;
        private System.Func<float, float, float> _easing;

        #endregion

        #region Constructors
        /// <summary>
        /// The constructor creates a ColorChange modifier to be used by an emitter.
        /// </summary>
        public ChangeColor()
            : this(Color.White, Color.White, null)
        {
        }

        /// <summary>
        /// The constructor creates a ColorChange modifier to be used by an emitter.
        /// </summary>
        /// <param name="startColor">
        /// The color of the particle with an energy of 1.0f. This is usually at the beginning of its life.
        /// </param>
        /// <param name="endColor">
        /// The color of the particle with an energy of 0.0f. This is usually at the end of its life.
        /// </param>
        public ChangeColor(Color startColor, Color endColor)
        {
            _startColor = startColor;
            _endColor = endColor;
            this._easing = starLiGHT.ParticleSystem.Common.EnergyEasing.Linear.easeNone;
        }

        /// <summary>
        /// The constructor creates a ColorChange modifier to be used by an emitter.
        /// </summary>
        /// <param name="startColor">
        /// The color of the particle with an energy of 1.0f. This is usually at the beginning of its life.
        /// </param>
        /// <param name="endColor">
        /// The color of the particle with an energy of 0.0f. This is usually at the end of its life.
        /// </param>
        /// <param name="easing">
        /// An easing function to use to modify the color curve over the lifetime 
        /// of the particle. A null value produces a linear response with no easing.
        /// </param>
        public ChangeColor(Color startColor, Color endColor, System.Func<float, float, float> easing)
        {
            this._startColor = startColor;
            this._endColor = endColor;

            if (easing == null)
            {
                this._easing = starLiGHT.ParticleSystem.Common.EnergyEasing.Linear.easeNone;
            }
            else
            {
                this._easing = easing;
            }
        }

        #endregion

        /// <summary>
        /// The color of the particle with an energy of 1.0f. This is usually at the beginning of its life.
        /// </summary>
        public Color StartColor
        {
            get { return this._startColor; }
            set { this._startColor = value; }
        }

        /// <summary>
        /// The color of the particle with an energy of 0.0f. This is usually at the end of its life.
        /// </summary>
        public Color EndColor
        {
            get { return this._endColor; }
            set { this._endColor = value; }
        }

        /// <summary>
        /// An easing function to use to modify the color curve over the lifetime 
        /// of the particle. A null value produces a linear response with no easing.
        /// </summary>
        public System.Func<float, float, float> Easing
        {
            get { return this._easing; }
            set { this._easing = value; }
        }

        public override void Update(Emitter emitter, Particle particle, float time)
        {
            float ratio = this._easing(particle.energy, 1.0f);
            particle.color = Utils.interpolateColors(this._startColor, this._endColor, ratio);
        }
    }
}
